package com.trg.designmodel.factory.Simplefactory.order;

import com.trg.designmodel.factory.Simplefactory.pizza.CheesPizza;
import com.trg.designmodel.factory.Simplefactory.pizza.GreekPizza;
import com.trg.designmodel.factory.Simplefactory.pizza.Pizza;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * @ClassName: OrderPizza
 * @Description: pizza订购
 * @Author: tangrg
 * @Date: 2021/7/1 10:27
 * @Version: 1.0
 * @Return:
 **/
public class OrderPizza {

    /**
     * 普通模式
     * @param simpleFactory
     */
//    public OrderPizza() {
//        Pizza pizza = null;
//        String pizzaType = null;
//        do {
//             pizzaType = getType();
//            if ("cheess".equals(pizzaType)) {
//                pizza = new CheesPizza();
//                pizza.setName("cheess");
//            } else if ("greek".equals(pizzaType)) {
//                pizza = new GreekPizza();
//                pizza.setName("greek");
//            } else {
//                break;
//            }
//            pizza.prepare();
//            pizza.bake();
//            pizza.cut();
//            pizza.box();
//        } while (true);
//    }


    /**
     * 2、简单工厂的方式
     *
     * @param simpleFactory
     */
    public OrderPizza(SimpleFactory simpleFactory) {
        setFactory(simpleFactory);
    }

    SimpleFactory simpleFactory;
    Pizza pizza;

    public void setFactory(SimpleFactory simpleFactory) {
        this.simpleFactory = simpleFactory;
        String pizzaType = null;
        do {
            pizzaType = getType();
            pizza = simpleFactory.createPizza(pizzaType);
            if (pizza != null) { //订购成功
                pizza.prepare();
                pizza.bake();
                pizza.cut();
                pizza.box();
            } else {
                System.out.println(" 订购披萨失败 ");
                break;
            }


        } while (true);

    }

    public String getType() {
        try {
            BufferedReader inpput = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入披萨种类：");
            String s = inpput.readLine();
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 3、静态工厂的方式
     *
     * @return
     */

    public static void setFactory2() {
        String pizzaType = null;
        do {
            pizzaType = getType2();
            Pizza pizza2 = SimpleFactory.createPizza2(pizzaType);
            if (pizza2 != null) { //订购成功
                pizza2.prepare();
                pizza2.bake();
                pizza2.cut();
                pizza2.box();
            } else {
                System.out.println(" 订购披萨失败 ");
                break;
            }


        } while (true);

    }


    public static String getType2() {
        try {
            BufferedReader inpput = new BufferedReader(new InputStreamReader(System.in));
            System.out.println("请输入披萨种类：");
            String s = inpput.readLine();
            return s;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


}
